home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
icon
/
packages.lha
/
packages
/
atari
/
ats.arc
/
TESTS.ARC
/
BTREES.ICN
< prev
next >
Wrap
Text File
|
1990-03-28
|
950b
|
44 lines
#
# B I N A R Y T R E E S
#
# This program accepts string representations of binary trees from
# standard input. It performs a tree walk and lists the leaves of
# each tree.
record node(data,ltree,rtree)
procedure main()
local line, tree
while line := read() do {
tree := tform(line)
write("tree walk")
every write(walk(tree))
write("leaves")
every write(leaves(tree))
}
end
procedure tform(s)
local value,left,right
if /s then return
s ? if value := tab(upto('(')) then {
move(1)
left := tab(bal(','))
move(1)
right := tab(bal(')'))
return node(value,tform(left),tform(right))
}
else return node(s)
end
procedure walk(t)
suspend walk(\t.ltree | \t.rtree)
return t.data
end
procedure leaves(t)
if not(\t.ltree | \t.rtree) then return t.data
suspend leaves(\t.ltree | \t.rtree)
end